package com.bawei.lib_storage.database

import com.bawei.lib_storage.database.entity.FavoriteVideoEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object FavoriteManager {
    
    /**
     * 添加收藏
     */
    suspend fun addFavorite(
        videoId: Int,
        title: String,
        description: String,
        coverUrl: String,
        videoUrl: String,
        userId: String
    ): Boolean = withContext(Dispatchers.IO) {
        try {
            val favoriteVideo = FavoriteVideoEntity(
                videoId = videoId,
                title = title,
                description = description,
                coverUrl = coverUrl,
                videoUrl = videoUrl,
                userId = userId
            )
            DBUtils.getDB().favoriteVideoDao().insert(favoriteVideo)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
    
    /**
     * 取消收藏
     */
    suspend fun removeFavorite(videoId: Int, userId: String): Boolean = withContext(Dispatchers.IO) {
        try {
            DBUtils.getDB().favoriteVideoDao().deleteByVideoIdAndUserId(videoId, userId)
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
    
    /**
     * 检查是否已收藏
     */
    suspend fun isFavorite(videoId: Int, userId: String): Boolean = withContext(Dispatchers.IO) {
        try {
            DBUtils.getDB().favoriteVideoDao().isFavorite(videoId, userId)
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
    
    /**
     * 获取用户的所有收藏
     */
    suspend fun getAllFavorites(userId: String): List<FavoriteVideoEntity> = withContext(Dispatchers.IO) {
        try {
            android.util.Log.d("FavoriteManager", "Getting all favorites for userId: $userId")
            val favorites = DBUtils.getDB().favoriteVideoDao().getAllByUserId(userId)
            android.util.Log.d("FavoriteManager", "Found ${favorites.size} favorites")
            favorites
        } catch (e: Exception) {
            e.printStackTrace()
            android.util.Log.e("FavoriteManager", "Error getting favorites", e)
            emptyList()
        }
    }
    
    /**
     * 切换收藏状态
     */
    suspend fun toggleFavorite(
        videoId: Int,
        title: String,
        description: String,
        coverUrl: String,
        videoUrl: String,
        userId: String
    ): Boolean = withContext(Dispatchers.IO) {
        try {
            android.util.Log.d("FavoriteManager", "Checking if video $videoId is favorite for user $userId")
            val isFavorite = DBUtils.getDB().favoriteVideoDao().isFavorite(videoId, userId)
            android.util.Log.d("FavoriteManager", "Current favorite status: $isFavorite")
            
            if (isFavorite) {
                // 如果已收藏，则取消收藏
                android.util.Log.d("FavoriteManager", "Removing favorite for video $videoId")
                DBUtils.getDB().favoriteVideoDao().deleteByVideoIdAndUserId(videoId, userId)
                false
            } else {
                // 如果未收藏，则添加收藏
                android.util.Log.d("FavoriteManager", "Adding favorite for video $videoId")
                val favoriteVideo = FavoriteVideoEntity(
                    videoId = videoId,
                    title = title,
                    description = description,
                    coverUrl = coverUrl,
                    videoUrl = videoUrl,
                    userId = userId
                )
                DBUtils.getDB().favoriteVideoDao().insert(favoriteVideo)
                true
            }
        } catch (e: Exception) {
            e.printStackTrace()
            android.util.Log.e("FavoriteManager", "Error toggling favorite", e)
            false
        }
    }
} 